package com.boc.ewr.risk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.boc.ewr.bcimp.entity.CorpEbkTlr;
import com.boc.ewr.bcimp.entity.Pro;
import com.boc.ewr.bcimp.service.ICorpEbkTlrService;
import com.boc.ewr.bcimp.service.IProService;
import com.boc.ewr.risk.entity.CaUnpriaccUnusual;
import com.boc.ewr.risk.entity.EbankOprateMismatchFdbk;
import com.boc.ewr.risk.mapper.EbankOprateMismatchFdbkMapper;
import com.boc.ewr.risk.service.IEbankOprateMismatchFdbkService;
import com.boc.ewr.util.ToUserUtil;
import org.jeecg.common.api.dto.message.MessageDTO;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.SysDepartModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 网银操作员不符清单
 * @Author: jeecg-boot
 * @Date: 2023-04-20
 * @Version: V1.0
 */
@Service
public class EbankOprateMismatchFdbkServiceImpl extends ServiceImpl<EbankOprateMismatchFdbkMapper, EbankOprateMismatchFdbk> implements IEbankOprateMismatchFdbkService {

    @Autowired
    private ICorpEbkTlrService corpEbkTlrService;

    @Autowired
    private ISysBaseAPI sysBaseAPI;

    @Autowired
    private IProService proService;

    /**
     * CASS系统中网银ID与交易银行部的对公客户网银操作员信息不符数据：
     * 使用PRO中的网银操作员ID信息与交易银行部的对公客户网银操作员信息相匹配中出现不符的数据。
     * 不需要做反馈，同一客户号下所有操作员ID均不符才进行提示
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createInfo() {
        //清空当前记录

        //生成数据
        // 按客户号顺序排列Pro数据
        List<Pro> proList = proService.list(
                new LambdaQueryWrapper<Pro>()
                        .orderByAsc(Pro::getCusmNo)
        );
        // 获取 交易银行部的对公客户网银操作员信息 中 全部 网银操作员ID信息
        List<String> ebkTlrIdList = corpEbkTlrService.list()
                .stream()
                .map(CorpEbkTlr::getEbkTlrId)
                .collect(Collectors.toList());

        // 将导入的Pro中的网银操作员Id 与上述IdList匹配，不匹配的生成 网银操作员不符清单
        // 同一客户号下所有操作员ID均不符才进行提示
        List<EbankOprateMismatchFdbk> ebankOprateMismatchFdbkList = new ArrayList<>();

        // 初始化 客户号
        String cusmNoMark = proList.get(0).getCusmNo();
        // 初始化操作员IdList
        List<String> netbankHandlerIdList = new ArrayList<>();
        Map<String, List<String>> map = new HashMap<>();
        for (Pro pro : proList) {
            if (!cusmNoMark.equals(pro.getCusmNo())) {
                // 储存进Map中
                map.put(cusmNoMark, netbankHandlerIdList);
                // 重置cusmNo
                cusmNoMark = pro.getCusmNo();
                // 重置List
                netbankHandlerIdList = new ArrayList<>();
            }
            netbankHandlerIdList.addAll(Arrays.asList(pro.getNetbankHandler()));
        }
        // 储存会后一次记录
        map.put(cusmNoMark, netbankHandlerIdList);

        Set<String> cusmNoSet = map.keySet();
        for (String cusmNo : cusmNoSet) {
            List<String> handlerIdList = map.get(cusmNo);
            boolean isContains = false;
            for (String handlerId : handlerIdList) {
                // 如果存在就直接跳过循环
                if (ebkTlrIdList.contains(handlerId)){
                    isContains = true;
                    break;
                }
            }
            if (!isContains){
                List<Pro> list = proService.list(
                        new LambdaQueryWrapper<Pro>()
                                .eq(Pro::getCusmNo, cusmNo)
                );
                for (Pro pro : list) {
                    EbankOprateMismatchFdbk ebankOprateMismatchFdbk = new EbankOprateMismatchFdbk();
                    String signedOrg = pro.getSignedOrg();
                    SysDepartModel sysDepartModel = sysBaseAPI.selectAllById(signedOrg);
                    ebankOprateMismatchFdbk.setAdminOrgNo(sysDepartModel.getParentId());
                    ebankOprateMismatchFdbk.setBranchNo(sysDepartModel.getId());
                    ebankOprateMismatchFdbk.setBranchNm(sysDepartModel.getDepartName());
                    ebankOprateMismatchFdbk.setCusmNo(pro.getCusmNo());
                    ebankOprateMismatchFdbk.setCusmNm(pro.getCusmName());
                    ebankOprateMismatchFdbk.setHandlerNo(pro.getNetbankHandler());

                    ebankOprateMismatchFdbkList.add(ebankOprateMismatchFdbk);
                }
            }
        }

        this.sendMsg(ebankOprateMismatchFdbkList);
        return this.saveBatch(ebankOprateMismatchFdbkList);
    }

    private void sendMsg(Collection<EbankOprateMismatchFdbk> entityList) {
        // 获取推送用户
        Set<String> branchNoSet = new HashSet();
        for (EbankOprateMismatchFdbk ebankOprateMismatchFdbk : entityList) {
            branchNoSet.add(ebankOprateMismatchFdbk.getBranchNo());
        }
        String toUser = ToUserUtil.genToUser(branchNoSet);
        if (toUser != null) {
            MessageDTO messageDTO = new MessageDTO(
                    "system",
                    toUser,
                    "网银操作员不符",
                    "网银操作员不符清单已更新，请及时查看"
            );
            sysBaseAPI.sendSysAnnouncement(messageDTO);
        }

    }
}
